<?php

class fily_ExpressionHelper {

    public static function isExpression($expression) {
        $is = false;
        $pos =stripos($expression, ':');
        if($pos !== false){
            $is = true;
        }

        return $is;
    }

    public static function getParts($expression) {
        return  split(':', $expression);
    }


    public static function next(array $expressionParts,  &$expression,fily_ControlToHtml $baseControl) {
        if($baseControl->getParent()!=null) {
            $control = $baseControl->getParent()->find(self::create(array ('after', $baseControl->getId())));
            $expression = self::create($expressionParts, 2);
        }
        return $control;
    }


    public static function previous(array $expressionParts,  &$expression,fily_ControlToHtml $baseControl) {
        if($baseControl->getParent()!=null) {
            $control = $baseControl->getParent()->find(self::create(array ('before', $baseControl->getId())));
            $expression = self::create($expressionParts, 2);
        }
        return $control;
    }

    public static function after(array $expressionParts, &$expression, fily_ControlList $controlList) {
        $control = null;
        if(count($expressionParts)>2) {
            $id =$expressionParts[2];
            $control = $controlList->getAfterId($id);
            $expression = self::create($expressionParts, 3);
        }
        return $control;
    }

    public static function before(array $expressionParts, &$expression, fily_ControlList $controlList) {
        $control = null;
        if(count($expressionParts)>2) {
            $id =$expressionParts[2];
            $control = $controlList->getBeforeId($id);
            $expression = self::create($expressionParts, 3);
        }
        return $control;
    }

    public static function parent(array $expressionParts, &$expression, fily_ControlToHtml $baseControl) {
        $control = $baseControl->getParent();
        $expression = self::create($expressionParts, 2);
        return $control;
    }

    public static function first(array $expressionParts, &$expression, fily_ControlList $controlList) {
        $control =$controlList->get(0);
        $expression =self::create($expressionParts, 2);
        return $control;
    }

    public static function last(array $expressionParts, &$expression, fily_ControlList $controlList) {
        $control = $controlList->get($controlList->count() - 1);
        $expression =self::create($expressionParts, 2);
        return $control;
    }


    public static function child(array $expressionParts, &$expression, fily_ControlList $controlList) {
        if(count($expressionParts)>2) {
            $i =$expressionParts[2];
            $control = $controlList->get($i );
            $expression =self::create($expressionParts, 3);
        }
        return $control;
    }


  public static function root(array $expressionParts,  &$expression,fily_ControlToHtml $baseControl) {
        if($baseControl->getParent()!=null) {
           $control = $baseControl->getParent()->find(':root');
           if($control==null){
               // baseControl is the root
               $control = $baseControl;
           }
            $expression = self::create($expressionParts, 2);
        }
        return $control;
    }


    public static function attr(array $expressionParts,  &$expression,fily_ControlList $controlList, fily_ControlToHtml $baseControl, fily_KeyValueArray  $attributes){
               $control = null;
               $attribut =$expressionParts[2];
               $value =$expressionParts[3];
               if($attributes->get($attribut) == $value){
                    $control = $baseControl;
               }
               else {
                   
               }
               return $control;
    }




    public static function create(array $expressionArray, $offset = 0) {
        $i = 1;
        $expression = '';
        foreach ($expressionArray as  $value) {
            if($i > $offset) {
                $expression .= ':' .$value;
            }
            $i++;
        }
        return $expression;
    }
}
?>
